<?php
/**
 * Created by PhpStorm.
 * User: Windows
 * Date: 2015/12/24
 * Time: 9:57
 */

class erpInquiryController extends Apicontroller{
    //初始化
    public function init(){
        parent::init();
    }

    /**
     * 这边的读取求购的信息
     */

    public function getInquiryAction(){
        global $_BuyStateCfg;
        $api = new ApiModel();

        $url = _HTTP_CLOUD_HOST."/api/Inquiry/getInquiry";
        //var_dump($url);
        $requestdata = $api->url_request($url);
        //var_dump(json_decode($requestdata,true));

        $data = json_decode($requestdata,true);
        if(!empty($data) && $data['status']=='success'){
            foreach($data['list'] as $key=>$var){

                if(!empty($var['product'])&&!empty($var['brand_name'])&&!empty($var['cate_name'])){
                    $product = new ProductModel();
                    //这边创建产品
                    $product_id = $product->getProdutByName($var['product'],$var['brand_name'],$var['cate_name']);
                    if(!$product_id){
                        //这边插入产品,
                        $product_id = $product->addProdutByName($var['product'],$var['brand_name'],$var['cate_name']);
                    }
                }


                if($var['status']=='draft'){
                    $status = $_BuyStateCfg['audit'];
                }else if($var['status']=='quoted'){
                    $status = $_BuyStateCfg['Negotiate'];
                } else{
//                    if($var['status']=='cancel'){
//                        $status = $_BuyStateCfg['audit'];
//                    }else{
                        $status = $_BuyStateCfg['done'];
//                    }
                }

                $data_list = array(
                    'user_id'=>$var['create_uid'],
                    'organization_id'=>$var['organization_id'],
                    'department_id'=>$var['department_id'],
                    'create_time'=>$var['create_time'],
                    'create_uid'=>$var['create_uid'],
                    'update_time'=>$var['update_time'],
                    'update_uid'=>$var['update_uid'],
                    'srouce_type'=>'erp',
                    'type'=>$var['type'],
                    'status'=>$status,
                    'quote_id'=>$var['quote_id'],
                    'product_id'=>$product_id,
                    'product_name'=>$var['product_name'],
                    'service_id'=>$var['service_id'],
                    'currency_id'=>$var['currency_id'],
                    'delivery_method'=>$var['delivery_method'],
                    'delivery_time'=>$var['delivery_time'],
                    'delivery_area'=>$var['delivery_area'],
                    'num'=>$var['num'],
                    'price'=>$var['price'],
                    'remark'=>$var['remark'],
                    'alert_num'=>$var['alert_num'],
                    'vice_uom_id'=>$var['vice_uom_id'],
                    'vice_rate'=>$var['vice_rate'],
                    'business_id'=>'',
                    'area_id'=>$var['region_id'],
                    'p_id'=>$var['id'],
                    'iq_contacts'=>mb_substr($var['realname'],0,1,'utf-8')."经理",
                    'iq_mobile'=>$var['mobile'],
                    'iq_qq'=>$var['qq'],

                );



                //这边无则插入，有责更新

                $inquiry = new InquireModel();
                $where = "iq.user_id = '{$var['create_uid']}' and iq.p_id='{$var['id']}' and iq.srouce_type='erp' and iq.type = '{$var['type']}' and iq.product_id = '{$product_id}' and iq.service_id = '{$var['service_id']}'  and iq.delivery_time = '{$var['delivery_time']}' and iq.area_id = '{$var['region_id']}'";
                $inquirylist = $inquiry->getInquireList($where);
				//var_dump($inquirylist);

                if(!empty($inquirylist)){

                    $inquiry_data = array(
                        'update_time'=>$var['create_time'],
                        'update_uid'=>$var['create_uid'],
                        'status'=>$status,
                        'iq_contacts'=>mb_substr($var['realname'],0,1,'utf-8')."经理",
                        'iq_mobile'=>$var['mobile'],
                        'iq_qq'=>$var['qq'],
                        'price'=>$var['price'],
                        'num'=>$var['num'],
                        'remark'=>'erp求购对接,重复数据进行更新',
                    );

                    $api->saveeditinquiry($inquiry_data,$inquirylist['id']);

                }else{

                    $api->addinquiry($data_list);

                }

                //这边的省略判断成功的结果

            }


        }else{
            echo "还没有满足的报价条件哦!";

        }
        die();

    }





    public function testAction(){
        echo "erpPrice-----test";
        die();
    }

}